<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="studentName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">学院</label>
                            <div class="layui-input-inline">
                                <input type="text" name="institute" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">专业</label>
                            <div class="layui-input-inline">
                                <input type="text" name="major" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">年级</label>
                            <div class="layui-input-inline">
                                <input type="text" name="grade" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">班级</label>
                            <div class="layui-input-inline">
                                <input type="text" name="clazz" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">联系方式</label>
                            <div class="layui-input-inline">
                                <input type="text" name="tel" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>


        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="watch">查看成绩</a>
        </script>

    </div>
</div>
<script src="../../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        // 存储当前搜索条件
        var searchData = {
            name: '',
            grade: '',
            tel: '',
            institute: '',
            major: '',
            clazz: ''
        };

        var currentPage = 1;
        var pageSize = 15;

        // 构建URL的辅助函数
        function buildUrl(page, size, searchParams) {
            function encode(val) {
                return val && val.trim() !== '' ? encodeURIComponent(val.trim()) : '_';
            }

            return '/students/' + page + '/' + size + '/' +
                encode(searchParams.name) + '/' +
                encode(searchParams.grade) + '/' +
                encode(searchParams.tel) + '/' +
                encode(searchParams.institute) + '/' +
                encode(searchParams.major) + '/' +
                encode(searchParams.clazz);
        }
        // 初始化表格
        table.render({
            elem: '#currentTableId',
            url: buildUrl(currentPage, pageSize, searchData), // 初始URL
            method: 'GET',
            response: {
                statusCode: 200
            },
            parseData: function(res) {
                console.log( res)
                return {
                    code: res.code,
                    msg: res.msg,
                    count: res.data.total,
                    data: res.data.records
                };
            },
            cols: [[
                {field: 'studentName', title: '姓名'},
                {field: 'institute', title: '学院'},
                {field: 'major', title: '专业'},
                {field: 'grade', title: '年级'},
                {field: 'clazz', title: '班级'},
                {field: 'tel', title: '联系方式'},
                {title: '操作', toolbar: '#currentTableBar', align: "center"}
            ]],
            page: {
                curr: currentPage,
                limit: pageSize,
                limits: [10, 15, 20, 50],
                layout: ['prev', 'page', 'next', 'limit', 'count'],
                groups: 5
            },
            skin: 'line'
        });

        // 搜索监听
        form.on('submit(data-search-btn)', function (data) {
            // 更新搜索条件
            searchData = {
                name: data.field.studentName || '',
                grade: data.field.grade || '',
                tel: data.field.tel || '',
                institute: data.field.institute || '',
                major: data.field.major || '',
                clazz: data.field.clazz || ''
            };

            // 重置到第一页
            currentPage = 1;

            // 重新加载表格
            table.reload('currentTableId', {
                url: buildUrl(currentPage, pageSize, searchData),
                method: 'GET',
                page: { curr: 1 }
            });

            return false;
        });

        // 监听分页切换
        table.on('page(currentTableFilter)', function(obj) {
            console.log('分页切换：当前页', obj.curr, '每页条数', obj.limit);

            // 更新当前页码和每页条数
            currentPage = obj.curr;
            pageSize = obj.limit;

            // 重新加载表格，保持搜索条件
            table.reload('currentTableId', {
                url: buildUrl(currentPage, pageSize, searchData),
                method: 'GET',
                page: {
                    curr: currentPage,
                    limit: pageSize
                }
            });
        });

        // 工具栏事件监听
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            console.log(data)
            if (obj.event === 'watch') {
                // 查看成绩的逻辑
                var index = layer.open({
                    title: '查看成绩',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['80%', '80%'],
                    content: 'studentScore.html?studentId=' + data.studentId // 假设有学生ID字段
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }
        });

    });
</script>

</body>
</html>